<template>
  <van-icon
    :color="value === 1 ? 'red' : ''"
    :name="value === 1 ? 'good-job' : 'good-job-o'"
    @click="onLike"
  />
</template>

<script>
import { addLike, deleteLike } from '@/api/article.js'
export default {
  data () {
    return {}
  },

  props: {
    value: {
      type: Number,
      required: true
    },
    articleId: {
      type: [Number, String, Object],
      required: true
    }
  },

  created () {},

  methods: {
    async onLike () {
      let status = 0
      if (this.value === 1) {
        // 已点赞，取消点赞
        await deleteLike(this.articleId)
        status = -1
      } else {
        // 未点赞，需要点赞
        await addLike(this.articleId)
        status = 1
      }
      this.$emit('input', status)
      this.$toast.success(status === 1 ? '点赞成功' : '取消点赞')
    }
  }
}
</script>

<style scoped lang="less"></style>
